1 using UnityEngine;
2 using
System.Collections;
3
4 public
class SoundManager{
5
6     
private static GameObject bgMusic;
7     
private static GameObject btSound;
8
9     
public static void LoadBgMusic(string fileName, bool isChangeMusic)
10     {
11         
if (!isMusic) return;
12         
if (isChangeMusic)
13         {
14             
if (bgMusic != null)
15             {
16                 Object.Destroy(bgMusic);
17                 bgMusic =
null;
18             }
19         }
20         
if (bgMusic == null)
21         {
22             bgMusic =
new GameObject("BgMusic");
23             AudioSource audio = bgMusic.AddComponent<AudioSource>();
24             audio.clip = Resources.Load<AudioClip>(fileName);
25             audio.loop =
true;
26             audio.Play();
27             Object.DontDestroyOnLoad(bgMusic);
28         }
29     }
30
31     
public static void playSound(string fileName)
32     {
33         
if (!isSound) return;
34         GameObject sound =
new GameObject("Sound");
35         AudioSource audio = sound.AddComponent<AudioSource>();
36         audio.clip = Resources.Load<AudioClip>(fileName);
37         audio.Play();
38         Object.Destroy(sound,
1);
39     }
40
41     
public static void playSoundLong(string fileName)
42     {
43         
if (!isSound) return;
44         GameObject sound =
new GameObject("Sound");
45         AudioSource audio = sound.AddComponent<AudioSource>();
46         audio.clip = Resources.Load<AudioClip>(fileName);
47         audio.Play();
48     }
49
50     
public static void playSoundLong(string fileName, float duration)
51     {
52         
if (!isSound) return;
53         GameObject sound =
new GameObject("Sound");
54         AudioSource audio = sound.AddComponent<AudioSource>();
55         audio.clip = Resources.Load<AudioClip>(fileName);
56         audio.Play();
57         Object.Destroy(sound, duration);
58     }
59
60     
public static void playButtonSound()
61     {
62         
//playSound("Sounds/click");
63         
if (!isSound) return;
64         
if (btSound != null)
65         {
66             btSound.GetComponent<AudioSource>().Play();
67         }
68         
else
69         {
70             btSound =
new GameObject("BtSound");
71             AudioSource audio = btSound.AddComponent<AudioSource>();
72             audio.clip = Resources.Load<AudioClip>(
"Sounds/click");
73             audio.Play();
74             Object.DontDestroyOnLoad(btSound);
75         }
76     }
77
78     
public static void stopMusic()
79     {
80         
if(bgMusic != null)
81             Object.Destroy(bgMusic);
82     }
83
84     
public static void PauseMusic()
85     {
86         
if (isMusic)
87         {
88             
if (bgMusic != null)
89             {
90                 bgMusic.GetComponent<AudioSource>().Pause();
91             }
92         }
93     }
94
95     
public static void ResumeMusic(string fileName)
96     {
97         
if (isMusic)
98         {
99             
if (bgMusic != null)
100             {
101                 bgMusic.GetComponent<AudioSource>().Play();
102             }
103             
else
104             {
105                 LoadBgMusic(fileName,
true);
106             }
107         }
108     }
109
110     
public static bool isMusic = true;
111     
public static bool isSound = true;
112 }


playSound("Soundsclick");




Trò chơi đua xe động vật trong UNITY Engine 114.687 lượt xem

Gõ tìm kiếm nhanh...